﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
namespace DoAn
{
    public class MapWithCells : VisibleGameEntity
    {
        protected float _scale = 1.0f;

        public float Scale
        {
            get { return _scale; }
            set
            {
                _scale = value;
                for (int i = 0; i < nRows; i++)
                    for (int j = 0; j < nCols; j++)
                    {
                        //cell[i, j].Scale = _scale;
                        cell[i, j].Size = cell[i, j].DefaultSize * _scale;
                    }
            }
        }

        protected Vector2 _defaultSize;

        public Vector2 DefaultSize
        {
            get { return _defaultSize; }
            set
            {
                _defaultSize = value;
            }
        }

        private int _nCols;

        public int nCols
        {
            get { return _nCols; }
            set { _nCols = value; }
        }
        private int _nRows;

        public int nRows
        {
            get { return _nRows; }
            set { _nRows = value; }
        }
        private Cell[,] _cell;

        public Cell[,] cell
        {
            get { return _cell; }
            set { _cell = value; }
        }

        public static int CELLWIDTH;
        public static int CELLHEIGHT;
        public static int WIDTHMAP;
        public static int HEIGHTMAP;
        public MapWithCells(ContentManager content, String strType, int cols, int rows, Vector2 topleft, int CellWidth, int cellheight)
        {
            CELLWIDTH = CellWidth;
            CELLHEIGHT = cellheight;
            Random r = new Random();
            nCols = cols;
            nRows = rows;
            cell = new Cell[rows, cols];
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                {
                    Vector2 vt = new Vector2();
                    vt.X = topleft.X + j * CELLWIDTH;
                    vt.Y = topleft.Y + i * CELLHEIGHT;
                    //if(i == 5 && (j > 5 && j < 15))
                    //    cell[i, j] = new Cell(content, strWater, vt, new Vector2(CELLWIDTH, CELLHEIGHT));
                    //else
                    cell[i, j] = new Cell(content, strType, vt, new Vector2(CELLWIDTH, CELLHEIGHT));
                }

            Size = new Vector2(CELLWIDTH * cols, CELLHEIGHT * rows);
            DefaultSize = Size;
            Topleft = topleft;
            WIDTHMAP = CELLWIDTH * cols;
            HEIGHTMAP = CELLHEIGHT * rows;
        }

        public MapWithCells(ContentManager content, String strxml)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strxml);
            XmlElement root = doc.DocumentElement;
            int nType = Convert.ToInt32(root.ChildNodes[0].InnerText);
            XmlNode node = root.ChildNodes[1];
            String str1 = node.ChildNodes[0].InnerText;
            String str2 = node.ChildNodes[1].InnerText;
            String str3 = node.ChildNodes[2].InnerText;
            String[] strType = { str1, str2, str3 };
            int cols = Convert.ToInt32(root.ChildNodes[2].InnerText);
            int rows = Convert.ToInt32(root.ChildNodes[3].InnerText);
            int top = Convert.ToInt32(root.ChildNodes[4].InnerText);
            int left = Convert.ToInt32(root.ChildNodes[5].InnerText);
            Vector2 topleft = new Vector2(top, left);

            Random r = new Random();
            nCols = cols;
            nRows = rows;
            cell = new Cell[rows, cols];
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                {
                    int idx = r.Next() % nType;
                    Vector2 vt = new Vector2();
                    vt.X = topleft.X + j * CELLWIDTH;
                    vt.Y = topleft.Y + i * CELLHEIGHT;
                    cell[i, j] = new Cell(content, strType[idx], vt, new Vector2(CELLWIDTH, CELLHEIGHT));
                }

            Size = new Vector2(CELLWIDTH * cols, CELLHEIGHT * rows);
            DefaultSize = Size;
            Topleft = topleft;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < nRows; i++)
                for (int j = 0; j < nCols; j++)
                {
                    cell[i, j].Draw(gameTime, spriteBatch);
                }
        }

        public override Vector2 Topleft
        {
            get { return _topleft; }
            set
            {
                _topleft = value;
                Vector2 size = cell[0, 0].Size;
                int x = (int)size.X;
                int y = (int)size.Y;
                for (int i = 0; i < nRows; i++)
                    for (int j = 0; j < nCols; j++)
                    {
                        Vector2 v = new Vector2(Topleft.X + j * x, Topleft.Y + i * y);
                        cell[i, j].Topleft = v;
                        Game1.a[i, j] = v + new Vector2(CELLWIDTH/2, CELLHEIGHT/2);
                    }
            }
        }

        public void ScrollMap(MouseState mouseState, int viewWidth, int viewHeight)
        {
            Vector2 tempmap = Topleft;
            if (mouseState.X < 20)
            {
                tempmap.X += 10;
            }
            if (mouseState.X > viewWidth - 56)
            {
                tempmap.X -= 10;
            }
            if (mouseState.Y < 20)
            {
                tempmap.Y += 10;
            }
            if (mouseState.Y > viewHeight - 50)
            {
                tempmap.Y -= 10;
            }

            Topleft = tempmap;

            Vector2 vt = Topleft;
            if (Topleft.X < -Size.X + viewWidth - 5)
                vt.X = -Size.X + viewWidth - 5;
            if (Topleft.Y < -Size.Y + viewHeight - 5)
                vt.Y = -Size.Y + viewHeight - 5;
            if (Topleft.X > 5)
                vt.X = 5;
            if (Topleft.Y > 5)
                vt.Y = 5;
            Topleft = vt;
        }
        public void ScrollMap(KeyboardState kbState, int viewWidth, int viewHeight)
        {
            Vector2 ufo = Topleft;
            if (kbState.IsKeyDown(Keys.Left) && ufo.X < 0)
            {
                ufo.X += 10;
                Topleft = ufo;
                MoveAll(new Vector2(10, 0));
            }
            if (kbState.IsKeyDown(Keys.Right) && ufo.X > (viewWidth - Size.X))
            {
                ufo.X -= 10;
                Topleft = ufo;
                MoveAll(new Vector2(-10, 0));
            }
            if (kbState.IsKeyDown(Keys.Up) && ufo.Y < 0)
            {
                ufo.Y += 10;
                Topleft = ufo;
                MoveAll(new Vector2(0, 10));
            }
            if (kbState.IsKeyDown(Keys.Down) && ufo.Y > (viewHeight - Size.Y - 130))
            {
                ufo.Y -= 10;
                Topleft = ufo;
                MoveAll(new Vector2(0, -10));
            }
        }
        public void MoveAll(Vector2 vt)
        {
            for (int i = 0; i < Game1.init.nSprites; i++)
            {
                Game1.init.mySprites[i].Position2 += vt;
                Game1.init.mySprites[i].Target2 += vt;
            }
            Game1.vectorTarget += vt;
            //for (int i = 0; i < 12; i++)
            //{
            //    Game1.DiaHinh[i].Position2 += vt;
            //}
        }
        public void MoveMap(Vector2 v)
        {
            Vector2 vt = new Vector2(Game1.VIEWWIDTH / 2, Game1.VIEWHEIGHT / 2 - 100) - v;
            Vector2 g = Topleft + vt;
            vt = g;
            if (g.X >= 0)
            {
                vt.X = 0;
            }
            if (g.X <= -(WIDTHMAP - Game1.VIEWWIDTH))
            {
                vt.X = -(WIDTHMAP - Game1.VIEWWIDTH);
            }
            if (g.Y >= 0)
            {
                vt.Y = 0;
            }
            if (g.Y <= -(HEIGHTMAP - Game1.VIEWHEIGHT))
            {
                vt.Y = -(HEIGHTMAP - Game1.VIEWHEIGHT);
            }
            Vector2 result = vt - Topleft;
            Topleft = Topleft + result;
            MoveAll(result);
        }
    }
}
